package ge.telasi.tasks.ui.user;

import ge.telasi.tasks.model.User;
import ge.telasi.tasks.ui.UIUtils;
import ge.telasi.tasks.ui.log.LoggerUtils;
import ge.telasi.tasks.ui.structure.StructureSelectDialog;
import java.awt.Desktop;
import java.net.URI;
import javax.swing.JPanel;

/**
 * @author dimitri
 */
public class UserPropertiesPanel extends JPanel {
    private static final long serialVersionUID = -1655015098967379043L;

    /** Creates new form UserPropertiesPanel */
    public UserPropertiesPanel() {
        initComponents();
        UIUtils.correctTitleBorderFont(jPanel1);
        UIUtils.correctTitleBorderFont(jPanel2);
        UIUtils.correctLabelFont(txtFullName);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        userImagePanel1 = new ge.telasi.tasks.ui.user.UserImagePanel();
        txtFullName = new javax.swing.JLabel();
        txtPosition = new javax.swing.JLabel();
        txtWorkPhone = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        hyperlink2 = new ge.telasi.tasks.ui.comp.Hyperlink();
        lnkGroup = new ge.telasi.tasks.ui.comp.Hyperlink();
        jPanel2 = new javax.swing.JPanel();
        hyperlink1 = new ge.telasi.tasks.ui.comp.Hyperlink();
        txtStructure = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtCode = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtLocation = new javax.swing.JTextField();
        txtEmail = new ge.telasi.tasks.ui.comp.Hyperlink();

        txtFullName.setFont(new java.awt.Font("DejaVu Sans", 1, 18));
        txtFullName.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        txtFullName.setText("fullname...");

        txtPosition.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        txtPosition.setText("position...");

        txtWorkPhone.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        txtWorkPhone.setText("phone...");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("ნახეთ აგრეთვე ამ მომხმარებელზე"));

        hyperlink2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ge/telasi/resorces/icons16x16/bricks.png"))); // NOI18N
        hyperlink2.setText("დამოკიდებულება");
        hyperlink2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                hyperlink2MouseClicked(evt);
            }
        });

        lnkGroup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ge/telasi/resorces/icons16x16/group.png"))); // NOI18N
        lnkGroup.setText("მომხმარებელთა ჯგუფები");
        lnkGroup.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lnkGroupMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lnkGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(hyperlink2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(251, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(hyperlink2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lnkGroup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("სად მუშაობს?"));

        hyperlink1.setText("სტრუქტ. ერთეული:");
        hyperlink1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                hyperlink1MouseClicked(evt);
            }
        });

        txtStructure.setEditable(false);

        jLabel9.setText("თანამშრომლის კოდი:");

        txtCode.setEditable(false);

        jLabel7.setText("ადგილმდებარეობა:");

        txtLocation.setEditable(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9)
                    .addComponent(hyperlink1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtLocation, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                    .addComponent(txtStructure, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                    .addComponent(txtCode, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hyperlink1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtStructure, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtLocation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        txtEmail.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        txtEmail.setText("email");
        txtEmail.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtEmailMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(userImagePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtFullName, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
                    .addComponent(txtPosition, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
                    .addComponent(txtWorkPhone, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
                    .addComponent(txtEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE))
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(userImagePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtFullName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtPosition)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtWorkPhone)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(9, 9, 9)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void hyperlink1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_hyperlink1MouseClicked
        if (user != null) {
            StructureSelectDialog dialog = new StructureSelectDialog(this);
            dialog.setStructure(user.getStructure());
            UIUtils.openDialog(dialog);
        }
    }//GEN-LAST:event_hyperlink1MouseClicked

    private void lnkGroupMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lnkGroupMouseClicked
        UserGroupDialog dialog = new UserGroupDialog(this);
        dialog.setUser(getUser());
        UIUtils.openDialog(dialog);
}//GEN-LAST:event_lnkGroupMouseClicked

    private void hyperlink2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_hyperlink2MouseClicked
        UserRelationViewDialog dialog = new UserRelationViewDialog(this);
        dialog.setUser(getUser());
        if (dialog.refresh()) {
            UIUtils.openDialog(dialog);
        }
    }//GEN-LAST:event_hyperlink2MouseClicked

    private void txtEmailMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtEmailMouseClicked
        String email = txtEmail.getText();
        try {
            Desktop.getDesktop().mail(new URI("mailto:" + email));
        } catch (Exception ex) {
            LoggerUtils.manageException(this, ex);
        }
    }//GEN-LAST:event_txtEmailMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private ge.telasi.tasks.ui.comp.Hyperlink hyperlink1;
    private ge.telasi.tasks.ui.comp.Hyperlink hyperlink2;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private ge.telasi.tasks.ui.comp.Hyperlink lnkGroup;
    private javax.swing.JTextField txtCode;
    private ge.telasi.tasks.ui.comp.Hyperlink txtEmail;
    private javax.swing.JLabel txtFullName;
    private javax.swing.JTextField txtLocation;
    private javax.swing.JLabel txtPosition;
    private javax.swing.JTextField txtStructure;
    private javax.swing.JLabel txtWorkPhone;
    private ge.telasi.tasks.ui.user.UserImagePanel userImagePanel1;
    // End of variables declaration//GEN-END:variables

    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
        reset();
    }

    private void reset() {
        if (user == null) {
            txtFullName.setText("");
            txtPosition.setText("");
            txtStructure.setText("");
            txtEmail.setText("");
            txtLocation.setText("");
            txtWorkPhone.setText("");
            txtCode.setText("");
        } else {
            txtFullName.setText(user.getFullName());
            txtPosition.setText(user.getPosition());
            txtStructure.setText(user.getStructure().getName());
            txtStructure.setCaretPosition(0);
            txtEmail.setText(user.getEmail());
            txtLocation.setText(user.getLocation());
            txtWorkPhone.setText("მობ. " + user.getMobile() + ", ტელ. " + user.getWorkPhone());
            txtCode.setText(user.getCode());
            userImagePanel1.setUser(user);
        }
    }
}
